package com.hotkid.ceo.jetpack.myjetpackmvvm.ui.login
import android.app.ActivityManager
import android.content.Intent
import android.content.pm.LauncherApps
import android.content.res.Configuration
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.PersistableBundle
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.gyf.immersionbar.ImmersionBar
import com.hotkid.ceo.jetpack.myjetpackmvvm.R
import com.hotkid.ceo.jetpack.myjetpackmvvm.base.BaseVmActivity
import com.hotkid.ceo.jetpack.myjetpackmvvm.common.MyConfig
import com.hotkid.ceo.jetpack.myjetpackmvvm.databinding.ActivityLoginBinding
import com.hotkid.ceo.jetpack.myjetpackmvvm.ui.main.MainActivity
import com.hotkid.ceo.jetpack.myjetpackmvvm.util.setOnclickNoRepeat
import com.yechaoa.yutilskt.ActivityUtil
import com.yechaoa.yutilskt.SpUtil
import com.yechaoa.yutilskt.ToastUtil
import com.yechaoa.yutilskt.YUtils

/**
 * ClassName:LoginActivity
 * Package:com.hotkid.ceo.jetpack.myjetpackmvvm.ui.login
 * Description:
 * @date:2021/3/25 1:59 PM
 * @author:774169396@qq.com
 */
class LoginActivity:BaseVmActivity<ActivityLoginBinding,LoginViewModel>() {
    override fun veiwModelClass()=LoginViewModel::class.java

    override fun getViewBinding()= ActivityLoginBinding.inflate(layoutInflater)

    override fun initView() {
        ImmersionBar
                .with(this)
                .fitsSystemWindows(true)
                .transparentBar()
                .statusBarDarkFont(true)
                .init()
           setText()
    }


    override fun setListeneres() {
        mBinding.tvRegister.setOnclickNoRepeat {
            // TODO: 2021/3/25 去注册页面
        }

        mBinding.btnLogin.setOnclickNoRepeat {
            if (!mBinding.cbServiceAgreement.isChecked){
                ToastUtil.show("同意服务协议与隐私协议后才能登录")
                return@setOnclickNoRepeat
            }
            attemptLogin()
        }
    }

    private fun attemptLogin() {
        YUtils.closeSoftKeyboard()

        mBinding.etUsername.error=null
        mBinding.etPassword.error= null

        val userName=mBinding.etUsername.text.toString().trim()
        val password=mBinding.etPassword.text.toString().trim()
        var  cacel=false
        var focusView:View?=null

         if (password.isEmpty()){
             mBinding.etPassword.error="密码不能为空"
             focusView=mBinding.etPassword
             cacel=true
         }

        if (userName.isEmpty()){
            mBinding.etUsername.error="密码不能为空"
            focusView=mBinding.etPassword
            cacel=true
        }


        if (cacel) focusView?.requestFocus() else doLogin(userName,password)
    }

    private fun doLogin(userName: String, password: String) {
        YUtils.showLoading(this,"加载中")
        mViewModel.login(userName,password)

    }

    private fun setText(){
        val spanBuilder=SpannableStringBuilder("同意")
        val color=ContextCompat.getColor(applicationContext, R.color.colorPrimary)
        var span= SpannableString("服务协议")
        span.setSpan(object :ClickableSpan(){
            override fun onClick(widget: View) {
                ToastUtil.show("服务协议")
            }

        },0,span.length,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        span.setSpan(ForegroundColorSpan(color),0,span.length,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        spanBuilder.append(span)

        spanBuilder.append("与")


        span.setSpan(object :ClickableSpan(){
            override fun onClick(widget: View) {
                ToastUtil.show("隐私政策")
            }

        },0,span.length,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        span.setSpan(ForegroundColorSpan(color),0,span.length,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        spanBuilder.append(span)

        mBinding.tvServiceAgreement.movementMethod=LinkMovementMethod.getInstance()
        mBinding.tvServiceAgreement.text=spanBuilder
        //设置高亮颜色透明，因为点击会变色
        mBinding.tvServiceAgreement.highlightColor=ContextCompat.getColor(applicationContext,R.color.transparent)
    }

    override fun onBackPressed() {
        ActivityUtil.closeAllActivity()

    }

    override fun observe() {
        super.observe()
        mViewModel.loginState.observe(this,{
            YUtils.hideLoading()
            SpUtil.setBoolean(MyConfig.IS_LOGIN,it)
            if (it){
                startActivity(Intent(this, MainActivity::class.java))
                finish()
            }else{
                ToastUtil.show("登录失败")
            }
        })


    }

}